<?php

require_once dirname(__FILE__)."/../common/sessionTimer.php";
require_once dirname(__FILE__)."/../common/objFile.php";

class FeedSessionState{
	public $companyName;
	public $provider;
	public $timestamp;
	public $skipRecord;
	
	const Idle = 0;
	const Running = 1;
	const Paused = 2;
	
	public $state;
	
	public function __construct($_coName = NULL, $_provider = NULL, $_timestamp = NULL, $_skip = NULL){
		$this->companyName = $_coName;
		$this->provider = $_provider;
		$this->skipRecord = $_skip;
		$this->timestamp = $_timestamp;
		$this->state = FeedSessionState::Idle;
	}
}

class FeedSession{
	private $feedSessionState;
	private $timer;
	private $fsFile;
	
	public function __construct(){
		$this->timer = new sessionTimer();
		//$this->feedSessionState = new FeedSessionState();
		$this->fsFile = new objFile("feedSession.raw");
		$this->feedSessionState = $this->fsFile->ReadInstance();
	}
	
	public function Load(){
		$this->feedSessionState = $this->fsFile->ReadInstance();
		return $this->GetCurrent();
	}
	
	public function GetCurrent(){
		return $this->feedSessionState;
	}
	
	public function SaveStep($fsState = NULL){
		if(isset($fsState))
			$this->feedSessionState = $fsState;
		$this->fsFile->SaveInstance($this->feedSessionState);
	}
	
	public function IsWaitingForStart(){
		if($this->feedSessionState->state == FeedSessionState::Idle)
			return true;
		
		return false;
	}
	
	public function Start(){
		if($this->feedSessionState->state == FeedSessionState::Idle){
			$this->feedSessionState->state = FeedSessionState::Running;
			$this->SaveStep();
		}
	}
	
	public function Pause(){
		if($this->feedSessionState->state == FeedSessionState::Running){
			$this->feedSessionState->state = FeedSessionState::Paused;
			$this->SaveStep();
		}
	}
	
	public function Resume(){
		if($this->feedSessionState->state == FeedSessionState::Paused){
			$this->Load();
			$this->feedSessionState->state = FeedSessionState::Running;
			$this->SaveStep();
		}
	}
	
	public function End(){
		if($this->feedSessionState->state == FeedSessionState::Running){
			$this->feedSessionState->state = FeedSessionState::Idle;
			$this->SaveStep();
		}
	}
	
	public function TimerElapsed(){
		return $this->timer->HasElapsed();
	}
	
	public function IsRunning(){
		if($this->feedSessionState->state == FeedSessionState::Running)
			return TRUE;
		return FALSE;
	}
}

?>
